package edu.swust.graduation_project.eauction.Adapter

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import edu.swust.graduation_project.eauction.Bean.ItemBean
import edu.swust.graduation_project.eauction.Bean.OrderBean
import edu.swust.graduation_project.eauction.R


class DeliveryListAdapter(private val mContext: Context, private val list: ArrayList<OrderBean>) :
    BaseAdapter(){
    private val mLayoutInflater: LayoutInflater = LayoutInflater.from(mContext)

    override fun getCount(): Int {
        return list.count()
    }

    override fun getItem(p0: Int): Any {
        return list[p0]
    }

    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }

    internal class ViewHolder {
        lateinit var imageView: ImageView
        lateinit var tvTitle: TextView
        lateinit var tvBuyer: TextView
        lateinit var tvSeller: TextView
        lateinit var tvCreateTime: TextView
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
        val holder: ViewHolder?
        var view = convertView
        if (view == null) {
            view = mLayoutInflater.inflate(R.layout.layout_list_order, null)
            holder = ViewHolder()
            holder.imageView = view.findViewById(R.id.iv_order_icon)
            holder.tvTitle = view.findViewById(R.id.tv_order_name)
            holder.tvBuyer = view.findViewById(R.id.tv_order_buyer)
            holder.tvSeller = view.findViewById(R.id.tv_order_seller)
            holder.tvCreateTime = view.findViewById(R.id.tv_order_create_time)
            view?.tag = holder
        } else {
            holder = view.tag as ViewHolder
        }
        //给控件赋值
        holder.tvTitle.text = list[position].item_name
        holder.tvCreateTime.text = list[position].item_create_time.toString().replace(".0", "")
        holder.tvBuyer.text = list[position].buyer
        holder.tvSeller.text = list[position].seller
        holder.imageView.let {
            mContext.let { it1 ->
                Glide.with(it1)
                    .load(
                        "*" +
                                holder.tvCreateTime.text.toString().replace(" ", "%20")
                                    .replace(":", "%3A") +
                                ".png"
                    )
                    .into(it)
            }
        }
        return view
    }

}
